import { Component, OnInit } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { pb } from 'src/pb';
import { ConnPage } from '../conn/conn.page';
import { HttpService } from '../http.service';

@Component({
  selector: 'app-conns',
  templateUrl: './conns.page.html',
  styleUrls: ['./conns.page.scss'],
})
export class ConnsPage implements OnInit {
  conns: pb.IConn[] = [];
  constructor(private http: HttpService, private modalCtrl: ModalController) {}

  async ngOnInit() {
    this.conns = await this.http.get('/api/conn');
  }

  async edit(conn: pb.IConn = {}) {
    const modal = await this.modalCtrl.create({
      component: ConnPage,
      componentProps: {
        conn,
      },
    });
    await modal.present();
    const { data } = await modal.onDidDismiss();
    if (data) {
      this.conns = data;
    } else {
      this.conns = await this.http.get('/api/conn');
    }
  }
}
